package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.exception.MyException;
import com.example.pojo.Attractions;
import com.example.pojo.Query;
import com.example.service.AttractionsService;
import com.example.utils.Page;
import com.example.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@CrossOrigin//解决跨域问题
@RestController
@RequestMapping("/attractions")
@Api(tags = "景点推荐")
public class AttractionsController {

    @Autowired
    private AttractionsService attractionsService;

    @GetMapping("/selectAllAttractions")
    public Result selectAllAttractions(Query query){
        Page<Attractions> page= new Page<>(query.getPageNum(),query.getPageSize(),attractionsService.count(query.getName()));
        page.setList(attractionsService.selectAllAttractions(query.getName(),page.getStart(),query.getPageSize()));
        Map<String, Object> map = new HashMap<>();
        map.put("list", page.getList());
        map.put("count", page.getTotalCount());//总记录数
        map.put("pageSize", query.getPageSize());//每页记录数
        map.put("pageNum", query.getPageNum());//当前页码
        return new Result<>("200", "查询成功", map);
    }

    @GetMapping("deleteAttractions")
    public Result deleteAttractions(Integer id){//删除景点
        int i= attractionsService.deleteAttractions(id);
        Map<String, Object> map = new HashMap<>();
        if (i>0){
            map.put("code",200);
            map.put("data",null);
            map.put("msg", "删除成功");
        }else {
            map.put("code",444);
            map.put("data",null);
            map.put("msg", "删除失败");
        }
        return new Result<>("222", "调用成功", map);
    }

    @PostMapping("/updateAttractions")
    public Result updateAttractions(@RequestBody Attractions attractions){//修改景点


        if (attractionsService.selectAttractionsById(attractions.getId()).getRecommend()==1){
            throw new MyException("444","该景点已经推荐了");
        }


        if (attractionsService.count(new QueryWrapper<Attractions>().eq("recommend",1))>=7){
            throw new MyException("444","景点推荐不能超过八个");
        }

        if (attractionsService.updateAttractions(attractions)<=0){
            throw new MyException("444","修改失败");
        }
        return new Result<>("222", "修改成功");
    }

    @PostMapping("/updateAttractions2")
    @ApiOperation("修改景点推荐")
    public Result updateAttractions2(@RequestBody Attractions attractions) {//修改景点
        if (attractionsService.selectAttractionsById(attractions.getId()).getRecommend()==0){
            throw new MyException("444","该景点没有推荐");
        }
        if (attractionsService.count(new QueryWrapper<Attractions>().eq("recommend",1))<5){
            throw new MyException("444","景点推荐不能少于四个");
        }
        if (attractionsService.updateAttractions2(attractions)<1){
            throw new MyException("444","修改失败");
        }
        return new Result<>("222", "修改成功");
    }




    @GetMapping("/selectAttractionsById")
    public Result selectAttractionsById(@RequestParam int id) {//根据id查询景点
        Attractions attractions = attractionsService.selectAttractionsById(id);
        Map<String, Object> map = new HashMap<>();
        if (attractions!=null){
            map.put("code",200);
            map.put("data",attractions);
            map.put("msg", "查询成功");
        }else {
            map.put("code",444);
            map.put("data",null);
            map.put("msg", "查询失败");
        }
        return new Result<>("222", "调用成功", map);
    }
}
